<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Archive_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	private $_folderImageStore = 'uploads/images/';
	function Archive_model()
	{
		parent::Model();
		
		$host = $this->db->hostname;
		$arrHost = explode(':',$host);
		
		$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
    function selfserving_admin_get_all_banners($status,$fromdate,$todate,$userid,$page,$numrow,$strUserId,$groupid)
	{
		$data = array();
        $data['info']=array();
        $data['total']=array();
        $lastday = date('d/m',strtotime('-1 day' ,strtotime(date('Y-m-d'))));
        $last2day = date('d/m',strtotime('-2 day' ,strtotime(date('Y-m-d'))));
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_get_all_banners('$status','$fromdate','$todate','$userid','$page','$numrow', '$strUserId','$groupid');") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        $CI = &get_instance();
        $CI->load->library('mycommon');
        while($row = mysqli_fetch_array($result))
		{
			switch($row['typead'])
			{
				case '0':
					$row['typead'] = 'CPM';
					break;
                case '1':
					$row['typead'] = 'CPC';
					break;
				default:
					break;
			}
			$row['filepath'] = '';
			$createdTime = date($row['createtime']);
			$createdTimeInfo = getdate(strtotime($createdTime));
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
			$row['filepath'] = $CI->mycommon->getBannerImagPath($yearFolder, $monthFolder, $row['filename']);			
            if($row['uviews']!='N/A')
            {
                $row['uviews']=number_format($row['uviews']);
            }
            if($row['clicks']!='N/A')
            {
                $row['clicks']=number_format($row['clicks']);
            }
            if($row['impressions']!='N/A')
            {
                $row['impressions']=number_format($row['impressions']);
            }
			$data['info'][] = $row;
        }
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['total'][] = $row1;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
    function selfserving_banner_update_inline($bannerid,$val,$type)
    {
        $userid=$this->session->userdata('uid');
		//$iconn = mysqli_connect($this->_host,$this->_username,$this->_password, $this->_dbname,$this->_port);
//		$result = mysqli_query($iconn, "CALL selfserving_banner_update_inline('$bannerid', '$val','$type','$userid');") or show_custom_error(mysqli_error($iconn));
//		mysqli_close($iconn);
        $iconn = $this->db->conn_id;
		$result = mysqli_multi_query($iconn, "CALL selfserving_banner_update_inline('$bannerid', '$val','$type','$userid');");
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $result;
    }
}